package com.taoyibao.hongcanting.ui.mine.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.taoyibao.hongcanting.R;
import com.taoyibao.hongcanting.base.fragment.BaseListFragment;
import com.taoyibao.hongcanting.cache.SpUtils;
import com.taoyibao.hongcanting.constant.CodeConstant;
import com.taoyibao.hongcanting.constant.NetCode;
import com.taoyibao.hongcanting.http.DefaultTransformer;
import com.taoyibao.hongcanting.http.JsonApiWrapper;
import com.taoyibao.hongcanting.http.ProgressSubscriber;
import com.taoyibao.hongcanting.model.CompanyBaseModel;
import com.taoyibao.hongcanting.model.OrderModel;
import com.taoyibao.hongcanting.model.OrderPageModel;
import com.taoyibao.hongcanting.ui.home.activity.PayActivity;
import com.taoyibao.hongcanting.ui.mine.activity.OrderDetailActivity;
import com.taoyibao.hongcanting.ui.mine.activity.WaitPayActivity;
import com.taoyibao.hongcanting.utils.CodeUtils;
import com.zhy.adapter.recyclerview.base.ViewHolder;

/**
 * @author TangAnna
 * @description: 订单列表
 * @date :${DATA} 14:08
 */
public class MineOrderFragment extends BaseListFragment<OrderModel> implements BaseListFragment.OnItemListener<OrderModel> {

    public static MineOrderFragment getInstance(int status) {
        Bundle bundle = new Bundle();
        bundle.putInt(CodeConstant.STATUS, status);
        MineOrderFragment fragment = new MineOrderFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        setBackgroundColor(CodeUtils.getIdColor(R.color.colorBg));
    }

    @Override
    protected void bindItemData(ViewHolder holder, final OrderModel orderModel, int position) {
        TextView tvCompanyname = holder.getView(R.id.tv_item_mineOrder_companyname);
        TextView tvpaystatus = holder.getView(R.id.tv_item_mineOrder_paystatus);
        TextView tvTime = holder.getView(R.id.tv_item_mineOrder_time);
        TextView tvAmount = holder.getView(R.id.tv_item_mineOrder_amount);
        TextView tvGoPay = holder.getView(R.id.tv_mineOrder_pay);
        View line = holder.getView(R.id.view_line);

        //店名
        if (!TextUtils.isEmpty(orderModel.order_companyname)) {
            tvCompanyname.setText(orderModel.order_companyname);
        }
        //状态  0未支付，1已支付
        switch (orderModel.order_paystatus) {
            case 0:
                tvpaystatus.setText(CodeUtils.getIdString(R.string.string_waitPay));
                line.setVisibility(View.VISIBLE);
                tvGoPay.setVisibility(View.VISIBLE);
                break;
            case 1:
                tvpaystatus.setText(CodeUtils.getIdString(R.string.string_finish));
                line.setVisibility(View.GONE);
                tvGoPay.setVisibility(View.GONE);
                break;
        }
        //时间
        if (!TextUtils.isEmpty(orderModel.order_addtime)) {
            tvTime.setText(orderModel.order_addtime);
        }
        //金额
        tvAmount.setText(CodeUtils.formatAmountUnit(orderModel.order_totalmoney));
        //去支付
        tvGoPay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PayActivity.open(getContext(), orderModel.order_id, SpUtils.getMemberId());
            }
        });
    }

    @Override
    public int bindItemView() {
        return R.layout.item_mine_oreder;
    }

    @Override
    public void loadData() {
        int status = getArguments().getInt(CodeConstant.STATUS);
        JsonApiWrapper.getApiService().userOrders(SpUtils.getToken(), SpUtils.getMemberId(), status, mCurrentPage, mPageSize)
                .compose(new DefaultTransformer<CompanyBaseModel<OrderPageModel>>())
                .subscribe(new ProgressSubscriber<CompanyBaseModel<OrderPageModel>>(getContext(), true) {
                    @Override
                    public void onNext(CompanyBaseModel<OrderPageModel> orderPageModel) {
                        if (orderPageModel.code == NetCode.HTTP_200) {
                            addData(orderPageModel.data.orders_info);
                            mTotalPage = orderPageModel.data.page_info.total_page;
                        } else {
                            finishRefreshAndLoadMore();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        setEmptyView(true);
                        finishRefreshAndLoadMore();
                    }
                });
        setOnItemListener(this);
    }

    /**
     * item 点击事件
     *
     * @param view
     * @param holder
     * @param position
     * @param orderModel
     */
    @Override
    public void itemClickListener(View view, RecyclerView.ViewHolder holder, int position, OrderModel orderModel) {
        if (orderModel.order_paystatus == CodeConstant.ORDER_STATUS_NOPAY) {
            WaitPayActivity.open(getContext(), orderModel.order_id);
        } else {
            OrderDetailActivity.open(getContext(), orderModel.order_id, SpUtils.getMemberId(), orderModel.order_paystatus);
        }
    }
}
